﻿package lobby
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import lobby.exchangeddata.HistoryInfoBase;
	import lobby.constants.ConstData;
	import flash.display.SimpleButton;
	import lobby.events.LobbyEvent;
	import lobby.exchangeddata.HashTable;
	
	/**
	 * ...
	 * @author Tom
	 */
	
	public class TablesPanel extends MovieClip
	{
		public static const DIST_X:Number = 3.0;
		public static const DIST_Y:Number = 3.5;
		public var mcMask:MovieClip;
		public var mcScroller:MovieClip;
		public var btnUp_scroller:SimpleButton;
		public var btnDown_scroller:SimpleButton;
		public var mcTables:MovieClip;
		private var tablesList:Array;
		private var scroll:CustomScrollBar;
		
		public function TablesPanel()
		{
			this.tabChildren = false;
			scroll = new CustomScrollBar(stage, mcTables, mcMask, mcScroller.mcTrack, mcScroller.mcButton, btnUp_scroller, btnDown_scroller, 15, false, false);
		}
		
		public function loadTables(tablesList:Array):void
		{
			//trace("loadTables" + tablesList.length);
			removeTables();
			var tb:TableMC;
			var info:HistoryInfoBase;
			var numBac:int = 0, add:int = 0;
			var row:int, col:int;
			this.tablesList = new Array();
			this.tablesList = tablesList;
			for (var n:int = 0; n < tablesList.length; n++)
			{
				info = tablesList[n];
				if (info.getGameType == ConstData.GAME_TYPE_BAC && !info.getTableClose)
				{
					numBac++;
					trace(info.getTableClose);
				}
			}
			
			if (numBac > 2)
			{
				// draw 3 bac table
				add = 1;
				var bac3:MovieClip = MovieClip(new Table3Bac());
				var panel:HistoryPanelBase = new HistoryPanelBase();
				panel.addChild(bac3);
				panel.name = "3bac";
				tb = new TableMC();
				tb.txtName.text = GlobalVariables.langInfo.hashTable.lookupString("3bac");
				tb.content.addChild(panel);
				tb.name = "3bac";
				tb.setCasinoId = null;
				tb.setGameTypeId = "Ba3";
				mcTables.addChild(tb);
			}
			
			for (var i:int = 0; i < tablesList.length; i++)
			{
				info = tablesList[i];
				row = (i + add) / 3;
				col = (i + add) % 3;
				tb = new TableMC();
				tb.x = col * (tb.width + DIST_X);
				tb.y = row * (tb.height + DIST_Y);
				tb.name = info.getTableId;
				tb.setCasinoId = info.getCasinoId;
				tb.setGameTypeId = info.getGameType;
				tb.txtName.text = tb.getGameTypeName() + " " + info.getTableName;
				if (!info.getTableClose)
				{
					drawTable(info, tb);
				}
				else
				{
					// draw closed table
					var closePanel:HistoryPanelBase = new HistoryPanelBase();
					closePanel.name = info.getTableId;
					var textMC:MovieClip = MovieClip(new TableClosed());
					textMC.name = "tableclosed";
					textMC["tableclose_txt"].text = GlobalVariables.langInfo.hashTable.lookupString("INFOclosetable");
					closePanel.addChild(textMC);
					tb.content.addChild(closePanel);
					tb.tbMask.mouseEnabled = false;
					tb.isClosed = true;
					mcTables.addChild(tb);
				}
			}
			
			scroll.update();
		}
		
		private function removeTables():void
		{
			var num:int = mcTables.numChildren;
			for (var i:int = 0; i < num; i++)
			{
				mcTables.removeChildAt(0);
			}
		}
		
		private function drawTable(info:HistoryInfoBase, tb:TableMC):void
		{
			var event:LobbyEvent = new LobbyEvent(LobbyEvent.UPDATE_HISTORY_VIEW, true);
			event.tableId = info.getTableId;
			event.gameType = info.getGameType;
			var panel:HistoryPanelBase;
			switch (info.getGameType)
			{
				case ConstData.GAME_TYPE_BAC: 
				case ConstData.GAME_TYPE_BAC_IN: 
					panel = new HistoryPanelBaccarat();
					break;
				case ConstData.GAME_TYPE_DT: 
					panel = new HistoryPanelDragonTiger();
					break;
				case ConstData.GAME_TYPE_FANTAN: 
					panel = new HistoryPanelFantan();
					break;
				case ConstData.GAME_TYPE_ROULETTE: 
					panel = new HistoryPanelRoulette();
					break;
				case ConstData.GAME_TYPE_SEDIE: 
					panel = new HistoryPanelSedie();
					break;
				case ConstData.GAME_TYPE_SICBO: 
					panel = new HistoryPanelSicbo();
					break;
			}
			
			panel.name = info.getTableId;
			panel.setHistory = info.history;
			panel.initBoard(info.history);
			tb.content.addChild(panel);
			mcTables.addChild(tb);
			if (info.history.length == 0)
			{
				dispatchEvent(event);
			}
		}
		
		public function updateTableHistory(tableId:String, dataHis:Array):void
		{
			//trace("updateTableHistory");
			function isTable(element:HistoryInfoBase, index:int, arr:Array):Boolean
			{
				return (element.getTableId == tableId);
			}
			var temp:Array = tablesList.filter(isTable);
			
			if (temp.length == 0)
				return;
			
			var info:HistoryInfoBase = new HistoryInfoBase();
			info = temp[0];
			var tb:TableMC = TableMC(mcTables.getChildByName(info.getTableId));
			
			if (tb.isClosed)
				return;
			
			var panel:HistoryPanelBase;
			var obj:DisplayObject = tb.content.getChildByName(info.getTableId);
			switch (info.getGameType)
			{
				case ConstData.GAME_TYPE_BAC: 
				case ConstData.GAME_TYPE_BAC_IN: 
					panel = HistoryPanelBaccarat(obj);
					break;
				case ConstData.GAME_TYPE_DT: 
					panel = HistoryPanelDragonTiger(obj);
					break;
				case ConstData.GAME_TYPE_FANTAN: 
					panel = HistoryPanelFantan(obj);
					break;
				case ConstData.GAME_TYPE_ROULETTE: 
					panel = HistoryPanelRoulette(obj);
					break;
				case ConstData.GAME_TYPE_SEDIE: 
					panel = HistoryPanelSedie(obj);
					break;
				case ConstData.GAME_TYPE_SICBO: 
					panel = HistoryPanelSicbo(obj);
					break;
			}
			panel.setHistory = info.history;
			panel.initBoard(info.history);
		}
		
		public function changeTextLang(info:HashTable):void
		{
			var tb:TableMC;
			for (var i:int = 0; i < mcTables.numChildren; i++)
			{
				tb = TableMC(mcTables.getChildAt(i));
				tb.changeTextLang();
			}
		}
		
		public function deleteTableHistory(tableId:String):void 
		{
			var tb:TableMC = TableMC(mcTables.getChildByName(tableId));
			if (tb != null) {
				trace("table found");
			var panel:HistoryPanelBase = HistoryPanelBase(tb.content.getChildByName(tableId));
			panel.deleteTableHistory();
		}
			else {
				trace("table not found");
			}
		}
	}

}
